#include "filesystem/be_filesystem.h"
#include "map.h"
#include "map_mesh.h"

Map::Map(entityType eType, boost::shared_ptr<btDynamicsWorld> ownerWorld) 
 : Entity(eType, ownerWorld),
finish_width(0.1f),
finish_height(0.1f),
m_skyBoxAngle(0)
{
	startgrid.setIdentity();
	finish.setIdentity();
}

Map::~Map()
{
	cerr << "exiting map" << endl;
};

boost::shared_ptr<Map> Map::LoadMap(const char* mapname, boost::shared_ptr<btDynamicsWorld> ownerWorld)
{
	boost::shared_ptr<Map> map(new MeshMap(ownerWorld));
	map->load(mapname);
	map->LoadSkybox("skys/default/skydome3.obj");
	return map;
}

void Map::LoadSkybox(const char* skyboxname)
{
	m_skyBoxModel=BeModelSystem::Instance()->load(skyboxname, btVector3(1,1,1));
}

void Map::update( const float timeDelta )
{
	if ( m_skyBoxModel )
	{
		m_skyBoxAngle += 1.0f*timeDelta;
	}
}

void Map::drawSkyBox()
{
	if( m_skyBoxModel )
	{
		BeGraphicsSystem graphicsSystem;
		graphicsSystem.matrixPush(GL_MODELVIEW);
		graphicsSystem.matrixTranslate(GL_MODELVIEW, m_transform.getOrigin().getX(), m_transform.getOrigin().getY(), m_transform.getOrigin().getZ());
		graphicsSystem.matrixRotate(GL_MODELVIEW, m_skyBoxAngle, 0, 1, 0);
		m_skyBoxModel->draw();
		graphicsSystem.matrixPop(GL_MODELVIEW);
	}
}
